package com.eleadmin.order.biz.impl;

import com.eleadmin.common.system.entity.Role;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.order.biz.ApplyCustomsBiz;
import com.eleadmin.order.entity.ApplyCustoms;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.order.param.ApplyCustomsParam;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-05-06 10:49:33
 */
@Repository
public class ApplyCustomsBizImpl extends BaseDao implements ApplyCustomsBiz {

    public static void main(String[] args) {
//        Date currentTime = new Date();
//        System.out.println(currentTime.getTime());
//        System.out.println(System.currentTimeMillis());
//        UUID uuid = UUID.randomUUID();
//        System.out.println(uuid.toString());
        Random random = new Random();
        int r = random.nextInt(999999) + (random.nextInt(9)+1) * 1000000;
        System.out.println(r);
        for (int i = 0; i < 10; i++) {

            System.out.println(System.currentTimeMillis());
//             r =  random.nextInt(999999) + (random.nextInt(9)+1) * 1000000;
//            System.out.println(r);
        }
    }

    @Override
    public JsonResult list(PageResult2<T> page, ApplyCustoms applyCustoms, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<ApplyCustoms> rm = new BeanPropertyRowMapper<ApplyCustoms>(ApplyCustoms.class);
        StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.customer_id");
        sql.append(",a.customer,a.customs_agent_id,a.customs_agent,a.customs_date");
        sql.append(",a.customs_place,a.bill_of_lading");
        sql.append(",a.annex,a.salesmanId");
        sql.append(",a.salesman,a.operaterId,a.operater,a.create_time");
        sql.append(",a.create_user,a.update_time,a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(",cc.containbill_id,cc.ocean_id ");
        sql.append(",cc.containbill_lading,cc.ocean_lading ");
        sql.append(" from chf_apply_customs a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=a.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
        sql.append(" left join chf_contain_ocean_apply cc on cc.apply_id=a.id ");
        sql.append(" where a.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and a.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(applyCustoms)) {
            if (Utils.isNotEmpty(applyCustoms.getKeywords())) {
                sql.append(" and (instr(a.customer,:keywords)>0 or instr(a.customs_agent,:keywords)>0 or instr(a.customs_date,:keywords)>0 " +
                        "or instr(a.customs_place,:keywords)>0 or instr(a.bill_of_lading,:keywords)>0 ) ");
                map.put("keywords", applyCustoms.getKeywords());
            }
            if (Utils.isNotEmpty(applyCustoms.getCustomer())) {
                sql.append(" and instr(a.customer,:customer)>0 ");
                map.put("customer", applyCustoms.getCustomer());
            }
            if (Utils.isNotEmpty(applyCustoms.getCustomsAgent())) {
                sql.append(" and instr(a.customs_agent,:customs_agent)>0 ");
                map.put("customs_agent", applyCustoms.getCustomsAgent());
            }
            if (Utils.isNotEmpty(applyCustoms.getBillOfLading())) {
                sql.append(" and instr(a.bill_of_lading,:bill_of_lading)>0 ");
                map.put("bill_of_lading", applyCustoms.getBillOfLading());
            }
            if (Utils.isNotEmpty(applyCustoms.getState())) {
                if (applyCustoms.getState().equals("未关联")) {
                    sql.append(" and  cc.apply_id is null  ");
                } else {
//                    sql.append(" and c.state=:state ");
//                    map.put("state", applyCustoms.getState());
                }
            }
            if (Utils.isNotEmpty(applyCustoms.getSort()) && Utils.isNotEmpty(applyCustoms.getOrder())) {
                sql.append(" order by a." + applyCustoms.getSort() + " " + applyCustoms.getOrder());
            } else {
                sql.append(" order by a.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<ApplyCustoms> list(ApplyCustomsParam applyCustoms, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<ApplyCustoms> rm = new BeanPropertyRowMapper<ApplyCustoms>(ApplyCustoms.class);
        StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.customer_id");
        sql.append(",a.customer,a.customs_agent_id,a.customs_agent,a.customs_date");
        sql.append(",a.customs_place,a.bill_of_lading");
        sql.append(",a.annex,a.salesmanId");
        sql.append(",a.salesman,a.operaterId,a.operater,a.create_time");
        sql.append(",a.create_user,a.update_time,a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_apply_customs a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
        sql.append(" where a.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and a.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        boolean salesman = false;
        boolean operator = false;
        for(Role role : loginUser.getRoles()) {
            if(role.getRoleName().equals("业务员")){
                salesman = true;
            }
            if(role.getRoleName().equals("操作")){
                operator = true;
            }
            if(role.getRoleName().indexOf("调度")>-1 || role.getRoleName().indexOf("财务")>-1
                    || role.getRoleName().indexOf("管理")>-1){
                operator = false;
                salesman = false;
            }
        }
        if (Utils.isNotEmpty(applyCustoms)) {
            if (salesman) {
                sql.append(" and a.salesmanId=:salesmanId ");
                map.put("salesmanId", loginUser.getUserId()+"");
            }
            if (operator) {
                sql.append(" and a.operaterId=:operaterId ");
                map.put("operaterId", loginUser.getUserId()+"");
            }
            if (Utils.isNotEmpty(applyCustoms.getFactoryPackDateSearch1())) {
                sql.append(" and a.customs_date>=:factoryPackDateSearch1 ");
                map.put("factoryPackDateSearch1", applyCustoms.getFactoryPackDateSearch1());
            }
            if (Utils.isNotEmpty(applyCustoms.getFactoryPackDateSearch2())) {
                sql.append(" and a.customs_date<=:factoryPackDateSearch2 ");
                map.put("factoryPackDateSearch2", applyCustoms.getFactoryPackDateSearch2());
            }
            if (Utils.isNotEmpty(applyCustoms.getCustomerEq())) {
                sql.append(" and a.customer=:customerEq ");
                map.put("customerEq", applyCustoms.getCustomerEq());
            }
            if (Utils.isNotEmpty(applyCustoms.getCustomer())) {
                sql.append(" and instr(a.customer,:customer)>0 ");
                map.put("customer", applyCustoms.getCustomer());
            }
            if (Utils.isNotEmpty(applyCustoms.getCustomsAgent())) {
                sql.append(" and instr(a.customs_agent,:customs_agent)>0 ");
                map.put("customs_agent", applyCustoms.getCustomsAgent());
            }
            if (Utils.isNotEmpty(applyCustoms.getBillOfLading())) {
                sql.append(" and instr(a.bill_of_lading,:bill_of_lading)>0 ");
                map.put("bill_of_lading", applyCustoms.getBillOfLading());
            }
            if (Utils.isNotEmpty(applyCustoms.getSort()) && Utils.isNotEmpty(applyCustoms.getOrder())) {
                sql.append(" order by a." + applyCustoms.getSort() + " " + applyCustoms.getOrder());
            } else {
                sql.append(" order by a.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public ApplyCustoms getById(String id) {
        RowMapper<ApplyCustoms> rm = new BeanPropertyRowMapper<ApplyCustoms>(ApplyCustoms.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select a.id,a.organization_id,a.customer_id");
        sql.append(",a.customer,a.customs_agent_id,a.customs_agent,a.customs_date");
        sql.append(",a.customs_place,a.bill_of_lading");
        sql.append(",a.annex,a.salesmanId");
        sql.append(",a.salesman,a.operaterId,a.operater,a.create_time");
        sql.append(",a.create_user,a.update_time,a.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_apply_customs a ");
        sql.append(" inner join sys_user suser on suser.user_id=a.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=a.organization_id and sogzn.deleted=0 ");
        sql.append(" where a.deleted=0 and a.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(ApplyCustoms applyCustoms, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into chf_apply_customs (");
        sql.append("organization_id,customer_id,customer,customs_agent_id");
        sql.append(",customs_agent,customs_date,customs_place,bill_of_lading");
        sql.append(",annex,salesmanId,salesman,operaterId");
        sql.append(",operater,create_user,create_time) values ( ");
        sql.append(":organization_id,:customer_id,:customer,:customs_agent_id");
        sql.append(",:customs_agent,:customs_date,:customs_place,:bill_of_lading");
        sql.append(",:annex,:salesmanId,:salesman,:operaterId");
        sql.append(",:operater,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("customer_id", applyCustoms.getCustomerId() == null ? null : applyCustoms.getCustomerId() + "");
        map.put("customer", applyCustoms.getCustomer());
        map.put("customs_agent_id", applyCustoms.getCustomsAgentId() == null ? null : applyCustoms.getCustomsAgentId() + "");
        map.put("customs_agent", applyCustoms.getCustomsAgent());
        map.put("customs_date", applyCustoms.getCustomsDate());
        map.put("customs_place", applyCustoms.getCustomsPlace());
        map.put("bill_of_lading", applyCustoms.getBillOfLading());
        map.put("annex", applyCustoms.getAnnex());
        map.put("salesmanId", applyCustoms.getSalesmanid() == null ? null : applyCustoms.getSalesmanid() + "");
        map.put("salesman", applyCustoms.getSalesman());
        map.put("operaterId", applyCustoms.getOperaterid() == null ? null : applyCustoms.getOperaterid() + "");
        map.put("operater", applyCustoms.getOperater());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(ApplyCustoms applyCustoms, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_apply_customs set ");
        sql.append("customer_id=:customer_id,customer=:customer,customs_agent_id=:customs_agent_id ");
        sql.append(",customs_agent=:customs_agent,customs_date=:customs_date,customs_place=:customs_place ");
        sql.append(",bill_of_lading=:bill_of_lading ");
        sql.append(",annex=:annex ");
        sql.append(",salesmanId=:salesmanId,salesman=:salesman,operaterId=:operaterId ");
        sql.append(",operater=:operater,update_time=now(),update_user=:update_user where id=:id ");
        map.put("customer_id", applyCustoms.getCustomerId() == null ? null : applyCustoms.getCustomerId() + "");
        map.put("customer", applyCustoms.getCustomer());
        map.put("customs_agent_id", applyCustoms.getCustomsAgentId() == null ? null : applyCustoms.getCustomsAgentId() + "");
        map.put("customs_agent", applyCustoms.getCustomsAgent());
        map.put("customs_date", applyCustoms.getCustomsDate());
        map.put("customs_place", applyCustoms.getCustomsPlace());
        map.put("bill_of_lading", applyCustoms.getBillOfLading());
        map.put("annex", applyCustoms.getAnnex());
        map.put("salesmanId", applyCustoms.getSalesmanid() == null ? null : applyCustoms.getSalesmanid() + "");
        map.put("salesman", applyCustoms.getSalesman());
        map.put("operaterId", applyCustoms.getOperaterid() == null ? null : applyCustoms.getOperaterid() + "");
        map.put("operater", applyCustoms.getOperater());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", applyCustoms.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update chf_apply_customs set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
